package com.ysstech.kettle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysstech.common.entity.PageBean;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.kettle.entity.JobCategory;
import com.ysstech.kettle.entity.JobTask;
import com.ysstech.kettle.mapper.JobTaskMapper;
import com.ysstech.kettle.service.JobCategoryService;
import com.ysstech.kettle.service.JobTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2022-10-11
 */
@Service
public class JobTaskServiceImpl extends ServiceImpl<JobTaskMapper, JobTask> implements JobTaskService {

    @Autowired
    private JobCategoryService jobCategoryService;
    @Autowired
    private JobTaskMapper jobTaskMapper;

    /**
     * 查询作业任务信息
     *
     * @param jobTask
     * @param pageBean
     * @return
     * @throws Exception
     */
    public PageBean<JobTask> queryAllJobTask(JobTask jobTask, PageBean pageBean) throws Exception {
        QueryWrapper<JobTask> wrapper = this.getQueryWrapper(jobTask);
        IPage pageBalance = this.page(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), wrapper);
        this.switchedListJob(pageBalance.getRecords());
        return new PageBean<>(pageBalance);
    }

    @Override
    public List<JobTask> queryJobTask(JobTask jobTask) throws Exception {
        return jobTaskMapper.queryJobTask(jobTask);
    }


    /**
     * 类型，机构等中文的转换
     *
     * @param list
     * @return
     * @throws Exception
     */
    public List<JobTask> switchedListJob(List<JobTask> list) throws Exception {
        List<JobCategory> categoryList = jobCategoryService.list();
        Map<String, String> map = new HashMap<>();
        for (JobCategory jobCategory : categoryList) {
            map.put(jobCategory.getId(), jobCategory.getCategoryName());
        }
        for (JobTask jobTask : list) {
            if (null == jobTask) {
                continue;
            }
            jobTask.setCategoryName(map.get(jobTask.getCategoryId()));
        }
        return list;
    }

    /**
     * 查询条件
     *
     * @param jobTask
     * @return
     * @throws Exception
     */
    private QueryWrapper<JobTask> getQueryWrapper(JobTask jobTask) throws Exception {
        QueryWrapper<JobTask> wrapper = new QueryWrapper<>();
        if (null == jobTask) {
            return wrapper;
        }
        if (StringUtils.isNotBlank(jobTask.getCategoryId())) {
            wrapper.eq("CATEGORY_ID", jobTask.getCategoryId());
        }
        if (StringUtils.isNotBlank(jobTask.getName())) {
            wrapper.like("NAME", jobTask.getName());
        }
        wrapper.orderByAsc("SEQUENCE");
        return wrapper;
    }
}
